package com.offlinerssreader.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;

import com.offlinerssreader.controller.ActionResponse;
import com.offlinerssreader.controller.ORRController;
import com.offlinerssreader.controller.ReturnableResponse;

/**
 * Servlet implementation class ApiRssFeed
 */
public class ApiRssFeed extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private ServletContext context = null;
	private ORRController controller = null;
	
	@Override
	public void init() {
		this.context = this.getServletContext();
		this.controller = (ORRController) this.context.getAttribute("controller");
		if (this.controller == null) {
			this.controller = new ORRController();
			this.context.setAttribute("controller", this.controller);
		}
	}

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ApiRssFeed() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		Integer userId;
		
		String call = request.getParameter("call");
		ReturnableResponse rtrn = new ActionResponse("nothing", true, "no msg", null);
		HttpSession session = request.getSession(false);
		
		if (session == null) {
			rtrn = new ActionResponse(call, false, "Un auhtorized access to API", null);
		} else {
			if (call == null) {
				rtrn = new ActionResponse("", false, "No call identifier found", null);
			} else {
				userId = (Integer)session.getAttribute("userId");
				if (userId == null) {
					// General error
					rtrn = new ActionResponse(call, false, "General error", null);
				} else {
					if (call.equals("new")) {
						String name = request.getParameter("name");
						String url = request.getParameter("url");
						
						if ((name == null) || (url == null)) {
							rtrn = new ActionResponse(call, false, "Some variables were not set", null);
						} else {
							rtrn = this.controller.addRssFeed(name, url, userId.intValue());
						}
					} else if (call.equals("delete")) {
						
						if (request.getParameter("id") == null) {
							rtrn = new ActionResponse(call, false, "Some variables were not set", null);
						} else {
							int id = Integer.parseInt(request.getParameter("id"));
							rtrn = this.controller.deleteRssFeed(id, userId.intValue());
						}
					} else if (call.equals("getFeedItems")) {
						int cache = Integer.parseInt(request.getParameter("cache"));
						int id = Integer.parseInt(request.getParameter("id"));
						if (cache == 0) {
							rtrn = this.controller.getFeedItems(id, userId.intValue(), false);
						} else {
							rtrn = this.controller.getFeedItems(id, userId.intValue(), true);
						}
					} else if (call.equals("refresh")) { 
						int id = Integer.parseInt(request.getParameter("id"));
							rtrn = this.controller.fetchFeeds(userId.intValue(), id);
					}else {
						rtrn = new ActionResponse(call, false, "Unsupported call in this API", null);
					}
				}
			}
		}
		
		try {
			writer.print(rtrn.toJson());
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
